/*
 * @Description:
 * @Author: yeke
 * @Date: 2023-09-24 09:33:12
 * @LastEditors: yeke
 * @LastEditTime: 2024-11-11 11:25:33
 */
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

import { ValidationPipe } from '@nestjs/common';

import { HttpExceptionFilter } from './common/http-exception.filter';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
async function bootstrap() {
  const port = 3011;
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  // 配置跨域
  app.enableCors({
    origin: true,
    methods: 'GET,PUT,POST',
    allowedHeaders: 'Content-Type,Authorization',
    exposedHeaders: 'Content-Range,X-Content-Range',
    credentials: true,
    maxAge: 3600,
  });

  // 配置swagger 描述
  const config = new DocumentBuilder()
    .setTitle('node-service-nest')
    .setDescription('nest服务端')
    .setVersion('1.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document);

  // 配置静态资源目录,访问示例:http://localhost:3001/public/video/1711609497296.mp4
  app.useStaticAssets(join(__dirname, 'public'), {
    prefix: '/public', // 虚拟前缀
  });

  // 配置全局管道
  app.useGlobalPipes(new ValidationPipe());

  // 配置全局过滤器
  app.useGlobalFilters(new HttpExceptionFilter());

  await app.listen(port);
  console.log(`localhost:${port}`);
}
bootstrap();
